package com.lemon.api.auto.ApiExecutor;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
表单提交器
*/
@Test(enabled = false)
public class FromSubmitApiExecutor {
    public static void doGet(String url, List<NameValuePair> parameters, String charset){
        String parameterString = URLEncodedUtils.format(parameters, charset);
        url +=("?"+parameterString);
        //接口url地址
/*        String url="http://test.lemonban.com/futureloan/mvc/api/member/login";
        String mobilephone="13517315120";
        String pwd="123456";
        url+=("?mobilephone="+mobilephone+"+&pwd="+pwd);*/
        //创建请求对象
        HttpGet httpGet = new HttpGet(url);
        //创建http客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //添加请求头指定数据格式
        httpGet.addHeader("Content-Type","application/x-www-form-urlencoded;charest=UTF-8");
        //发包
        try {
            //调用完接口获取到服务器的响应
            HttpResponse httpResponse = httpClient.execute(httpGet);
            //从响应对象中取出状态码
            int code = httpResponse.getStatusLine().getStatusCode();
            //从响应对象中取出响应报文
            String result = EntityUtils.toString(httpResponse.getEntity());
            //从响应对象中取出响应头
            String responseHeadersString = Arrays.toString(httpResponse.getAllHeaders());
            System.out.println("状态码:["+code+"]");
            System.out.println("响应报文:["+result+"]");
            System.out.println("响应头:["+responseHeadersString+"]");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @Test(enabled = false)
    public static void doPost(String url, List<NameValuePair>parameters,String charset){
        //创建请求对象
        HttpPost httpPost = new HttpPost(url);
        //创建http客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //发包
        try {
//            List<BasicNameValuePair> paramters = new ArrayList<BasicNameValuePair>();
            //设置请求头
            httpPost.addHeader(new BasicHeader("Content-Type","application/x-www-form-urlencoded;charest=UTF-8"));
            //准备参数，将参数设置到请求体中
            //UrlEncodedFormEntity:以表单的方式传递数据（mobilephone=13517315120&pwd=123456）
            String parameterString = URLEncodedUtils.format(parameters, charset);
            httpPost.setEntity(new StringEntity(parameterString,charset));
            //调用完接口获取到服务器的响应
            HttpResponse httpResponse = httpClient.execute(httpPost);
            //从响应对象中取出状态码
            int code = httpResponse.getStatusLine().getStatusCode();
            //从响应对象中取出响应报文
            String result = EntityUtils.toString(httpResponse.getEntity());
            //从响应对象中取出响应头
            String responseHeadersString = Arrays.toString(httpResponse.getAllHeaders());
            System.out.println("状态码:["+code+"]");
            System.out.println("响应报文:["+result+"]");
            System.out.println("响应头:["+responseHeadersString+"]");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
@Test
    public void test() {
        String url="http://test.lemonban.com/futureloan/mvc/api/member/login";
        List<NameValuePair> paramters=new ArrayList<NameValuePair>();
        paramters.add(new BasicNameValuePair("mobilephone","13517315120"));
        paramters.add(new BasicNameValuePair("pwd","123456"));
        String charset ="UTF-8";
//        doPost(url,paramters,charset);
        doGet(url,paramters,charset);
    }

}
